package com.zzk.lanouhn.myapplication1.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.picasso.Picasso;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.adapters.SingleGoodArticlesAdapter;
import com.zzk.lanouhn.myapplication1.adapters.SingleGoodPriceAdapter;
import com.zzk.lanouhn.myapplication1.adapters.SingleGoodShowAdapter;
import com.zzk.lanouhn.myapplication1.adapters.SingleGoodWellAdapter;
import com.zzk.lanouhn.myapplication1.modles.SingleGoodDetails;
import com.zzk.lanouhn.myapplication1.utils.HttpUtils;
import com.zzk.lanouhn.myapplication1.view.ScrollRecyclerView;

/**
 * 单品详情页面
 * Created by lanouhn on 16/9/12.
 */
public class SingleShopDetailsActivity extends AppCompatActivity {
    private ImageButton btn_back;
    private ImageView shop_image, iv_up, iv_down, brand_pic, iv_upProduct, iv_downProduct;
    private TextView tv_products, tv_show_counts,tv_articles_counts;
    private TextView tv_title1, tv_title2, tv_likes, tv_contents, tv_minprice, brand_country, brand_desc, brand_name1, brand_name2;
    private RelativeLayout rel_price, rel_place, rel_show,rel_article;
    private SingleGoodDetails singleGood;//单品详情实体类
    private int flag = 0;//判断点击状态
    private ScrollRecyclerView priceRecyclerView, showRecyclerView,articleRecyclerView;
    private RecyclerView wellRecyclerView;
    private SingleGoodPriceAdapter priceAdapter;
    private SingleGoodShowAdapter showAdapter;
    private SingleGoodArticlesAdapter articlesAdapter;
    private SingleGoodWellAdapter wellAdapter;
    private String singleId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_singleshopdetails);
        initView();
        initEvent();
        initData();
        getData();
    }

    private void initView() {
        btn_back = (ImageButton) findViewById(R.id.back_act_shopetails);
        shop_image = (ImageView) findViewById(R.id.imageBig_act_shopetails);
        tv_title1 = (TextView) findViewById(R.id.titleOne_act_shopetails);
        tv_title2 = (TextView) findViewById(R.id.titleTwo_act_shopetails);
        tv_likes = (TextView) findViewById(R.id.likes_act_shopetails);
        tv_contents = (TextView) findViewById(R.id.content_act_shopetails);
        tv_minprice = (TextView) findViewById(R.id.price_act_shopdetails);
        tv_products = (TextView) findViewById(R.id.country_cocontent_act_shopdetails);
        tv_show_counts = (TextView) findViewById(R.id.tv_show_counts_single_shop);
        tv_articles_counts= (TextView) findViewById(R.id.tv_articles_counts_single_shop);

        iv_up = (ImageView) findViewById(R.id.icon_priceTwo_act_shopdetails);
        iv_down = (ImageView) findViewById(R.id.icon_priceOne_act_shopdetails);
        iv_downProduct = (ImageView) findViewById(R.id.icon_countryOne_act_shopdetails);
        iv_upProduct = (ImageView) findViewById(R.id.icon_countryTwo_act_shopdetails);
        brand_pic = (ImageView) findViewById(R.id.shopPhoto_act_shopdetails);
        brand_country = (TextView) findViewById(R.id.country_act_shopdetails);
        brand_name1 = (TextView) findViewById(R.id.logo_act_shopdetails);
        brand_name2 = (TextView) findViewById(R.id.logoTwo_act_shopdetails);

        rel_price = (RelativeLayout) findViewById(R.id.rel_price_act_shopdetails);
        rel_place = (RelativeLayout) findViewById(R.id.rel_country_act_shopdetails);
        rel_show = (RelativeLayout) findViewById(R.id.rl_show_shop);
        rel_article= (RelativeLayout) findViewById(R.id.rl_article_shop);

        priceRecyclerView = (ScrollRecyclerView) findViewById(R.id.price_scRecyclerView_act_shopdetails);
        showRecyclerView = (ScrollRecyclerView) findViewById(R.id.scRecyclerView_show_act_shopdetails);
        articleRecyclerView= (ScrollRecyclerView) findViewById(R.id.scRecyclerView_essay_act_shopdetails);
        wellRecyclerView= (RecyclerView) findViewById(R.id.well_recyclerView_act_shopdetails);

    }

    private void initData(){
        Bundle bundle=this.getIntent().getExtras();
        singleId= (String) bundle.get("singleId");
    }
    private void initEvent() {
        rel_price.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (flag) {
                    case 0:
                        iv_down.setVisibility(View.INVISIBLE);
                        iv_up.setVisibility(View.VISIBLE);
                        flag = 1;
                        priceRecyclerView.setVisibility(View.VISIBLE);
                        break;
                    case 1:
                        iv_up.setVisibility(View.INVISIBLE);
                        iv_down.setVisibility(View.VISIBLE);
                        flag = 0;
                        priceRecyclerView.setVisibility(View.GONE);
                        break;
                }
            }
        });
        rel_place.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (flag) {
                    case 0:
                        iv_downProduct.setVisibility(View.INVISIBLE);
                        iv_upProduct.setVisibility(View.VISIBLE);
                        flag = 1;
                        tv_products.setVisibility(View.VISIBLE);
                        break;
                    case 1:
                        iv_upProduct.setVisibility(View.INVISIBLE);
                        iv_downProduct.setVisibility(View.VISIBLE);
                        flag = 0;
                        tv_products.setVisibility(View.GONE);
                        break;
                    default:

                        break;
                }
            }
        });
        rel_show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(SingleShopDetailsActivity.this,AllShowGoodsActivity.class);
                intent.putExtra("singleId",singleId);
                startActivity(intent);
            }
        });
        rel_article.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(SingleShopDetailsActivity.this,AllRelationActivity.class);
                intent.putExtra("singleId",singleId);
                startActivity(intent);
            }
        });
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void getData() {
        String Url = "http://open4.bantangapp.com/products/brandProduct/info?";
        String dataUrl = "app_id=com.jzyd.BanTang" +
                "&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d" +
                "&track_user_id=&oauth_token=&track_deviceid=863121028526879" +
                "&track_device_info=MI+2S&channel_name=xiaomi&app_installtime=1473128006" +
                "&app_versions=5.8.7&os_versions=5.0.2&screensize=720&v=19&id="+singleId;
        try {
            HttpUtils.doPostAsyn(Url, dataUrl, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    singleGood = gson.fromJson(result, SingleGoodDetails.class);
                    if (!singleGood.getData().equals("")) {
                        handler.sendEmptyMessage(1);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    Picasso.with(SingleShopDetailsActivity.this).load(singleGood.getData().getPics().get(0)).into(shop_image);
                    Picasso.with(SingleShopDetailsActivity.this).load(singleGood.getData().getBrand().getPic()).into(brand_pic);
                    tv_title1.setText(singleGood.getData().getBrand().getName());
                    tv_title2.setText(singleGood.getData().getTitle());
                    tv_likes.setText(singleGood.getData().getLikes()+"人喜欢");
                    tv_contents.setText(singleGood.getData().getDesc());
                    tv_minprice.setText(singleGood.getData().getMin_price());
                    tv_products.setText(singleGood.getData().getBrand().getDesc());
                    brand_country.setText(singleGood.getData().getBrand().getCountry());
                    brand_name1.setText(singleGood.getData().getBrand().getName());
                    brand_name2.setText(singleGood.getData().getBrand().getAlias());

                    priceAdapter = new SingleGoodPriceAdapter(SingleShopDetailsActivity.this, singleGood);
                    priceRecyclerView.setAdapter(priceAdapter);
                    LinearLayoutManager manager1 = new LinearLayoutManager(SingleShopDetailsActivity.this);
                    manager1.setOrientation(LinearLayoutManager.VERTICAL);
                    priceRecyclerView.setLayoutManager(manager1);

                    if (!singleGood.getData().getPost_count().equals("")) {
                        int count=Integer.valueOf(singleGood.getData().getPost_count());
                        rel_show.setVisibility(View.VISIBLE);
                        tv_show_counts.setText("用户晒单"+"("+count+")");
                        showAdapter = new SingleGoodShowAdapter(SingleShopDetailsActivity.this, singleGood);
                        showRecyclerView.setAdapter(showAdapter);
                        LinearLayoutManager manager2 = new LinearLayoutManager(SingleShopDetailsActivity.this);
                        manager2.setOrientation(LinearLayoutManager.VERTICAL);
                        showRecyclerView.setLayoutManager(manager2);
                    }

                    String count1=singleGood.getData().getArticle_topic_count();
                    tv_articles_counts.setText("相关文章"+"("+count1+")");
                    articlesAdapter=new SingleGoodArticlesAdapter(SingleShopDetailsActivity.this,singleGood);
                    articleRecyclerView.setAdapter(articlesAdapter);
                    LinearLayoutManager manager3 = new LinearLayoutManager(SingleShopDetailsActivity.this);
                    manager3.setOrientation(LinearLayoutManager.VERTICAL);
                    articleRecyclerView.setLayoutManager(manager3);

                    wellAdapter=new SingleGoodWellAdapter(SingleShopDetailsActivity.this,singleGood);
                    wellRecyclerView.setAdapter(wellAdapter);
                    LinearLayoutManager manager4 = new LinearLayoutManager(SingleShopDetailsActivity.this);
                    manager4.setOrientation(LinearLayoutManager.HORIZONTAL);
                    wellRecyclerView.setLayoutManager(manager4);
                    break;
                default:

                    break;
            }
        }
    };


}
